package ywh.model
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import ywh.event.StreamEvent;
	import ywh.manager.Core;
	import ywh.player.media.LngPicsShower;
	import ywh.player.RelayoutSprite;
	import ywh.pool.PlayerPool;
	import ywh.pool.StreamPool;
	import ywh.stream.MediaStream;
	import ywh.ui.TreeMenu;
	
	/**
	 * ...
	 * @author 杨伟华
	 */
	public class Model2TitlePanel extends RelayoutSprite
	{
		public var cnBtn:MovieClip;
		public var enBtn:MovieClip;
		public var prevTitleBtn:MovieClip;
		public var nextTitleBtn:MovieClip;
		public var titleTxt:TextField;
		public var bg:Sprite;
		private var stream:MediaStream;
		public function Model2TitlePanel()
		{
			
		}
		
		public function startSync():void
		{			
			initBtnEvent();
			toggleLng(cnBtn);
			stream = StreamPool.getStreamById("treemenu");
			Core.addEventListener(StreamEvent.STREAM_LIST_CHANGE_ITEM, streamListChangeItem);
		}
		
		private function initBtnEvent():void
		{
			cnBtn.buttonMode = true;			
			enBtn.buttonMode = true;
			prevTitleBtn.buttonMode = true;			
			nextTitleBtn.buttonMode = true;
			this.addEventListener(MouseEvent.MOUSE_DOWN, this_mouseDown);
		}
		
		private function this_mouseDown(e:MouseEvent):void 
		{
			switch(e.target) {
				case cnBtn:
				case enBtn:
					toggleLng(e.target as MovieClip);
					break;
				case prevTitleBtn:
					stream.prevItem();
					break;
				case nextTitleBtn:
					stream.nextItem();
					break;
			}
		}
		
		private function streamListChangeItem(e:StreamEvent):void
		{
			if (e.stream == stream)
			{
				var item:Object = (e.stream.shower.render as TreeMenu).selectedItem;
				titleTxt.text = item.orderLabel + ". " + item.label;
			}
		
		}

		private function toggleLng(btn:MovieClip):void
		{
			switch (btn)
			{
				case cnBtn: 
					(PlayerPool.getPlayerById("picsshow") as LngPicsShower).lng = LngPicsShower.CN;
					break;
				case enBtn: 
					(PlayerPool.getPlayerById("picsshow") as LngPicsShower).lng = LngPicsShower.EN;
					break;
				default: 
					return;
			}
			enBtn.gotoAndStop("off");
			cnBtn.gotoAndStop("off");
			cnBtn.mouseEnabled = true;
			enBtn.mouseEnabled = true;
			btn.gotoAndStop("on");
			btn.mouseEnabled = false;
		}
		
		override protected function handleSetRectangle(rec:Rectangle):void
		{
			super.handleSetRectangle(rec);
			horizantalLayout(rec.width, 5, [10, prevTitleBtn, 80, titleTxt, nextTitleBtn], [cnBtn, 20, enBtn, 10], null);
			bg.width = rec.width;
		}
	}

}